package com.lx.algorithm.radix;

import java.util.Arrays;

/**
 * Description: 桶排序 优化版
 * Copyright:   Copyright (c)2019
 * Company:     zefu
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2021-09-19 11:59:35
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2021-09-19     张李鑫                     1.0         1.0 Version
 */
public class RadixSort {


    /**
     * <p>
     * 桶排序 原思路
     * 基于某个位排序 从各位开始 准备十个桶
     * 分别把个位对应的数字 放到对应的桶里 倒出
     * <p>
     * 优化版本:
     * 准备一个与原数组长度相等的数组
     * 准备一个长度为10的数组
     * 准备一个累加和数组
     * <p>
     * 整体思路是用累加和去代替桶 其他思路基本一致 可以节省空间
     * </p>
     *
     * @param
     * @return int[]
     * @Author zlx
     * @date 2021/9/19
     */
    public static int[] radixSort(int[] arr) {
        if (null == arr || arr.length < 2) {
            return arr;
        }
        //先找出最大值
        int max = 0;
        for (int i = 0; i < arr.length; i++) {
            max = Math.max(max, arr[i]);
        }
        //找到最大值的位数
        int count = 0;
        while (max > 0) {
            max = max / 10;
            count++;
        }
        //位数
        int right = 1;
        //准备三个辅助数组
        int[] help = new int[arr.length];


        while (right <= count) {
            //准备好两个辅助数组
            int[] bucket = new int[10];

            //先遍历数组 把各位的数字放到对应的数组里计次
            for (int i = 0; i < arr.length; i++) {
                int digit = getDigit(arr[i], right);
                bucket[digit]++;
            }
            //累加和
            for (int i = 1; i < bucket.length; i++) {
                bucket[i] = bucket[i - 1] + bucket[i];
            }
            //计算累加和把新排序的数组放到新的辅助数组里去
            for (int i = arr.length - 1; i >= 0; i--) {
                int digit = getDigit(arr[i], right);
                int bucketCount = bucket[digit]--;
                help[bucketCount - 1] = arr[i];
            }
            for (int i = 0; i < arr.length; i++) {
                arr[i] = help[i];
            }
            right++;
        }

        return arr;
    }

    // for test

    public static void main(String[] args) {

        int testTime = 500000;
        int maxSize = 100;
        int maxValue = 100000;
        boolean succeed = true;
        for (int i = 0; i < testTime; i++) {
            int[] arr1 = generateRandomArray(maxSize, maxValue);
            int[] arr2 = copyArray(arr1);
            int[] arr3 = copyArray(arr1);
            radixSort(arr1);
            comparator(arr2);
            if (!isEqual(arr1, arr2)) {
                succeed = false;
                printArray(arr3);
                printArray(arr1);
                printArray(arr2);
                break;
            }
        }
        System.out.println(succeed ? "Nice!" : "Fucking fucked!");


    }

    // for test
    public static void comparator(int[] arr) {
        Arrays.sort(arr);
    }

    // for test
    public static int[] generateRandomArray(int maxSize, int maxValue) {
        int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) ((maxValue + 1) * Math.random());
        }
        return arr;
    }

    // for test
    public static int[] copyArray(int[] arr) {
        if (arr == null) {
            return null;
        }
        int[] res = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            res[i] = arr[i];
        }
        return res;
    }

    // for test
    public static boolean isEqual(int[] arr1, int[] arr2) {
        if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
            return false;
        }
        if (arr1 == null && arr2 == null) {
            return true;
        }
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }

    // for test
    public static void printArray(int[] arr) {
        if (arr == null) {
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + ",");
        }
        System.out.println();
    }

    public static int getDigit(int x, int d) {
        return ((x / ((int) Math.pow(10, d - 1))) % 10);
    }

}